package com.bootdo.wx.controller;

import com.bootdo.wx.domain.CmsSlideDO;
import com.bootdo.wx.domain.GoodsGoodsDO;
import com.bootdo.wx.service.CmsSlideService;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 轮播图
 * 
 * @author sys
 * @email 1992lcg@163.com
 * @date 2019-03-19 16:39:21
 */
 
@Controller
@RequestMapping("/wx/cmsSlide")
public class CmsSlideController {
	@Autowired
	private CmsSlideService cmsSlideService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	
	@GetMapping()
	@RequiresPermissions("wx:cmsSlide:cmsSlide")
	String CmsSlide(){
	    return "wx/cmsSlide/cmsSlide";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:cmsSlide:cmsSlide")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CmsSlideDO> cmsSlideList = cmsSlideService.list(query);
		if (cmsSlideList!=null){
			for (CmsSlideDO slideDO:cmsSlideList){
				String goodsId = slideDO.getGoodsId();
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(goodsId);
				if (goodsGoodsDO!=null){
					slideDO.setGoodsId(goodsGoodsDO.getName());
				}

			}
		}
		int total = cmsSlideService.count(query);
		PageUtils pageUtils = new PageUtils(cmsSlideList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:cmsSlide:add")
	String add(){
	    return "wx/cmsSlide/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:cmsSlide:edit")
	String edit(@PathVariable("id") String id,Model model){
		CmsSlideDO cmsSlide = cmsSlideService.get(id);
		model.addAttribute("cmsSlide", cmsSlide);
		List<Map<String,Object>> list = goodsGoodsService.queryAllForIdAndName(new HashMap<String,Object>());
		model.addAttribute("goodsList", list);
	    return "wx/cmsSlide/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:cmsSlide:add")
	public R save( CmsSlideDO cmsSlide){
		if (StringUtils.isBlank(cmsSlide.getImgUrl())){
			return R.error("请上传轮播图");
		}
		cmsSlide.setCreateAt(new Date());
		cmsSlide.setDelFlag(0);
		cmsSlide.setType(0);
		cmsSlide.setId(UuidUtil.get32UUID());
		if(cmsSlideService.save(cmsSlide)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:cmsSlide:edit")
	public R update( CmsSlideDO cmsSlide){
		if (StringUtils.isBlank(cmsSlide.getImgUrl())){
			return R.error("请上传轮播图");
		}
		cmsSlide.setCreateAt(new Date());
		cmsSlideService.update(cmsSlide);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:cmsSlide:remove")
	public R remove( String id){
		if(cmsSlideService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:cmsSlide:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		cmsSlideService.batchRemove(ids);
		return R.ok();
	}
	
}
